/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.migrate.cbc;

import com.wtkj.exception.BusinessException;
import com.wtkj.vo.dzzf.base.kfwd.Account;
import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.dao.DataInitilizingDao;
import com.wttech.dzzf.process.dao.ImportHisDataDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.user.User;
import com.wttech.dzzf.process.service.initilize.AccountProcessService;
import com.wttech.dzzf.process.service.initilize.FundAccountProcessService;
import com.wttech.dzzf.process.service.user.UserDetailInfosCreatorService;
import com.wttech.dzzf.process.util.BusinessUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 合并处理资金账户
 *
 * @author MaShibo
 * @date 2017/12/25.
 */
@Service
public class MergateFundAccountService {

	@Resource
	private ImportHisDataDao importHisDataDao;

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private DataInitilizingDao dataInitilizingDao;

	@Resource
	private UserDetailInfosCreatorService userDetailInfosCreatorService;

	@Resource
	private FundAccountProcessService fundAccountProcessService;

	@Resource
	private AccountProcessService accountProcessService;


	@Transactional(rollbackFor = Exception.class)
	public void process(FundAccount fundAccount) throws InvocationTargetException, IllegalAccessException {

		String fundAccountId = fundAccount.getFundAccountId();
		String certifiNo = fundAccount.getCompanyLic();
		Integer certifiType = fundAccount.getCertifiType();
		Integer ownerType = fundAccount.getOwnerType();

		//查询相同证件号、证件类型、topClass的资金账户信息
		FundAccount dzzfFund = importHisDataDao.queryLastestFundAccount(certifiNo, certifiType);

		//合并证件号、证件类型相同的资金账户
		if (dzzfFund != null) {
			updateFundAccount(dzzfFund, fundAccount);
			importHisDataDao.updateAccountFundAccountId(dzzfFund.getFundAccountId(), fundAccountId);
			importHisDataDao.deleteFundAccount(fundAccountId);
			fundAccountId = dzzfFund.getFundAccountId();
		}

		//根据证件号、证件类型查询客户信息
		User user = retrieveUserInfo(certifiNo, BusinessUtils.convertUserIdType(certifiType, ownerType), ownerType);
		User newUser = userDetailInfosCreatorService.buildUserInfo(fundAccount);
		User finalUser;
		if (user != null) {
			String userId = user.getId();
			BeanUtils.copyProperties(user, newUser);
			user.setId(userId);
			user.setOperation(UserConstants.UPDATE_OPERATION);
			finalUser = user;
			transferUserInfoDao.updateDzzfObject(user);
		} else {
			finalUser = newUser;
			fundAccountProcessService.createUserOrCompanyInfo(fundAccount, finalUser);
			transferUserInfoDao.createTestObjcet(finalUser);
		}

		List<Account> accounts = dataInitilizingDao.queryAccountsByFundAccountId(fundAccountId);
		accounts.forEach(account -> accountProcessService.processAccount(account, finalUser));
	}

	private User retrieveUserInfo(String userIdNum, Integer userIdType, Integer ownerType) {
		User user;
		if (ownerType == UserConstants.OLD_OWNERTYPE_USER) {
			user = importHisDataDao.queryPersonUser(userIdType, userIdNum);
		} else if (ownerType == UserConstants.OLD_OWNERTYPE_COMPANY) {
			user = importHisDataDao.queryCompanyUser(userIdType, userIdNum);
		} else {
			throw new BusinessException("错误的用户类型");
		}
		return user;
	}

	/**
	 * 更新电子支付中资金账户信息
	 *
	 * @param dzzfFund    电子支付资金账户
	 * @param fundAccount 新提供的资金账户
	 */
	private void updateFundAccount(FundAccount dzzfFund, FundAccount fundAccount) {
		dzzfFund.setMobile(fundAccount.getMobile());
		dzzfFund.setPhone(fundAccount.getPhone());
		dzzfFund.setAddress(fundAccount.getAddress());
		dzzfFund.setRelationName(fundAccount.getRelationName());
		dzzfFund.setAgentIdType(fundAccount.getAgentIdType());
		dzzfFund.setAgentIdNum(fundAccount.getAgentIdNum());
		dzzfFund.setBankAccountNo(fundAccount.getBankAccountNo());
		transferUserInfoDao.updateDzzfObject(dzzfFund);
	}

}
